package com.example.statistics.ui.main

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.example.statistics.R
import com.example.statistics.bean.PurchaseAgentBean
import com.example.statistics.ex.httpGet
import com.example.statistics.ex.slimAdapter
import com.example.statistics.net.UrlConfig
import com.example.statistics.ui.purchase.PurchaseProductListActivity
import com.example.statistics.utils.Constants
import kotlinx.android.synthetic.main.fragment_purchase.*
import org.jetbrains.anko.sdk27.coroutines.onClick
import org.jetbrains.anko.support.v4.startActivity

class PurchaseFragment : Fragment() {


    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        return inflater.inflate(R.layout.fragment_purchase, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        activity?.httpGet<PurchaseAgentBean>(UrlConfig.PURCHASE_AGENT_DATA) {
            it?.let { bean ->
                tv_sales_number.text = "已售 | " + bean.salesNumber.toString()
                tv_split_number.text = "拆柜 | " + bean.divideNumber.toString()
                tv_stock_number.text = "库存 | " + bean.stockNumber.toString()
                tv_transform_number.text = "运输 | " + bean.transportNumber.toString()
            }
        }
        layout_sales.onClick {
            startActivity<PurchaseProductListActivity>()
        }
        layout_split.onClick {
            startActivity<PurchaseProductListActivity>()
        }
        layout_stock.onClick {
            startActivity<PurchaseProductListActivity>()
        }
        layout_transform.onClick {
            startActivity<PurchaseProductListActivity>()
        }

        initAdapter()
    }

    private fun initAdapter() {
        val adapter = slimAdapter<PurchaseAgentBean>(R.layout.list_item_common_display) { bean ->
            val text = "库存:${bean.stockNumber} | " +
                    "拆柜:${bean.divideNumber} | " +
                    "运输:${bean.transportNumber} | " +
                    "出库:${bean.salesNumber}"
            this.text(R.id.tv_title, bean.agent)
                .text(R.id.tv_subtitle, text)
                .clicked {
                    startActivity<PurchaseProductListActivity>(Constants.DATA to bean)
                }
        }.attachTo(recycler_view)

        activity?.httpGet<List<PurchaseAgentBean>>(UrlConfig.PURCHASE_AGENT_LIST) {
            adapter.setDataList(it.orEmpty())
        }
    }
}